<?php
/**
 * Workaround for PHP < 5.3.0
 */
if(!function_exists('date_diff')) {
    class CDate {
        public $y;
        public $m;
        public $d;
        public $h;
        public $i;
        public $s;
        public $w;
        public $invert;
        
        public function __construct($string=''){
        	if ($string)
        		$this->create_from_date_string($string);
        }
        
		public function create_from_date_string($string){
			$pattern='/^P((\d+)Y)?((\d+)N)?((\d+)D)?((\d+)H)?((\d+)M)?((\d)+S)?$/';
			preg_match($pattern, $string,$output);
			$this->y = $output[2] 	? $output[2]  : 0;
			$this->m = $output[4] 	? $output[4]  : 0;
			$this->d = $output[6]	? $output[6]  : 0;
			$this->h = $output[8]	? $output[8]  : 0;
			$this->i = $output[10] 	? $output[10] : 0;
			$this->s = $output[12]	? $output[12] : 0;
		}

        public function format($format) {
            $format = str_replace('%R%y', ($this->invert ? '-' : '+') . $this->y, $format);
            $format = str_replace('%R%m', ($this->invert ? '-' : '+') . $this->m, $format);
            $format = str_replace('%R%d', ($this->invert ? '-' : '+') . $this->d, $format);
            $format = str_replace('%R%h', ($this->invert ? '-' : '+') . $this->h, $format);
            $format = str_replace('%R%i', ($this->invert ? '-' : '+') . $this->i, $format);
            $format = str_replace('%R%s', ($this->invert ? '-' : '+') . $this->s, $format);
            $format = str_replace('%R%w', ($this->invert ? '-' : '+') . $this->w, $format);
            
            $format = str_replace('%y', $this->y, $format);
            $format = str_replace('%m', $this->m, $format);
            $format = str_replace('%d', $this->d, $format);
            $format = str_replace('%h', $this->h, $format);
            $format = str_replace('%i', $this->i, $format);
            $format = str_replace('%s', $this->s, $format);
            $format = str_replace('%w', $this->w, $format);
            
            return $format;
        }
		
		public function datetime(){
			return new DateTime("$this->m-$this->d-$this->y $this->h:$this->i:$this->s");
		}
    }

    function date_diff(DateTime $date1, DateTime $date2) {
        $diff = new CDate();
        if($date1 > $date2) {
            $tmp = $date1;
            $date1 = $date2;
            $date2 = $tmp;
            $diff->invert = true;
        }
        
        $diff->y = ((int) $date2->format('Y')) - ((int) $date1->format('Y'));
        $diff->m = ((int) $date2->format('n')) - ((int) $date1->format('n'));
        if($diff->m < 0) {
            $diff->y -= 1;
            $diff->m = $diff->m + 12;
        }
        $diff->d = ((int) $date2->format('j')) - ((int) $date1->format('j'));
        if($diff->d < 0) {
            $diff->m -= 1;
            $diff->d = $diff->d + ((int) $date1->format('t'));
        }
        $diff->h = ((int) $date2->format('G')) - ((int) $date1->format('G'));
        if($diff->h < 0) {
            $diff->d -= 1;
            $diff->h = $diff->h + 24;
        }
        $diff->i = ((int) $date2->format('i')) - ((int) $date1->format('i'));
        if($diff->i < 0) {
            $diff->h -= 1;
            $diff->i = $diff->i + 60;
        }
        $diff->s = ((int) $date2->format('s')) - ((int) $date1->format('s'));
        if($diff->s < 0) {
            $diff->i -= 1;
            $diff->s = $diff->s + 60;
        }
        
        return $diff;
    }

	function date_add(DateTime $date, $string) {
       $add = new CDate($string);
	   $aDate = getdate(mktime(	
	   	$date->format('G') + $add->h,
        $date->format('i') + $add->i,
        $date->format('s') + $add->s,
        $date->format('n') + $add->m,
        $date->format('j') + $add->d,
        $date->format('Y') + $add->y
       ));
       $add->y = $aDate['year'];
       $add->m = $aDate['mon'];
       $add->d = $aDate['mday'];
       $add->h = $aDate['hours'];
       $add->i = $aDate['minutes'];
       $add->s = $aDate['seconds'];
	   $add->w = $aDate['wday'];	
       return $add;
	}


	function date_sub(DateTime $date, $string) {
       $sub = new CDate($string);
	   $aDate = getdate(mktime(	
	   	$date->format('G') - $sub->h,
        $date->format('i') - $sub->i,
        $date->format('s') - $sub->s,
        $date->format('n') - $sub->m,
        $date->format('j') - $sub->d,
        $date->format('Y') - $sub->y
       ));
       $sub->y = $aDate['year'];
       $sub->m = $aDate['mon'];
       $sub->d = $aDate['mday'];
       $sub->h = $aDate['hours'];
       $sub->i = $aDate['minutes'];
       $sub->s = $aDate['seconds'];
	   $sub->w = $aDate['wday'];	
       return $sub;
	}

}
?>